package com.ruoyi.yuwen.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.newAdd.returnResult.Data;
import com.ruoyi.common.newAdd.returnResult.Res;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yuwen.domain.Address;
import com.ruoyi.yuwen.service.IAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收货地址管理Controller
 * 
 * @author ruoyi
 * @date 2023-02-27
 */
@RestController
@RequestMapping("/yuwen/address")
public class AddressController extends BaseController
{
    @Autowired
    private IAddressService addressService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询收货地址管理列表
     */
    @PreAuthorize("@ss.hasPermi('yuwen:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(Address address)
    {
        startPage();
        List<Address> list = addressService.selectAddressList(address);
        return getDataTable(list);
    }

    /**
     * 导出收货地址管理列表
     */
    @PreAuthorize("@ss.hasPermi('yuwen:address:export')")
    @Log(title = "收货地址管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Address address)
    {
        List<Address> list = addressService.selectAddressList(address);
        ExcelUtil<Address> util = new ExcelUtil<Address>(Address.class);
        util.exportExcel(response, list, "收货地址管理数据");
    }

    /**
     * 获取收货地址管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('yuwen:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(addressService.selectAddressById(id));
    }

    /**
     * 新增收货地址管理
     */
    @PreAuthorize("@ss.hasPermi('yuwen:address:add')")
    @Log(title = "收货地址管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Address address)
    {
        return toAjax(addressService.insertAddress(address));
    }

    /**
     * 修改收货地址管理
     */
    @PreAuthorize("@ss.hasPermi('yuwen:address:edit')")
    @Log(title = "收货地址管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Address address)
    {
        return toAjax(addressService.updateAddress(address));
    }

    /**
     * 删除收货地址管理
     */
    @PreAuthorize("@ss.hasPermi('yuwen:address:remove')")
    @Log(title = "收货地址管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(addressService.deleteAddressByIds(ids));
    }


    /** 前端接口 **************************************************************************************************** */
    /** 前端接口 **************************************************************************************************** */
    /** 前端接口 **************************************************************************************************** */


    /**
     * 添加地址（若为默认地址，需先清楚其他地址默认状态，下同）
     * @param address
     * @return
     */
    @PostMapping("/front/addressAdd")
    public Res addressAdd(@RequestBody Address address){
        Long userId = tokenService.getUserId();
        if (address.getIsDefault()==1){
            addressService.updateAddressByUserId(userId);
        }
        address.setUserId(userId);
        addressService.insertAddress(address);
        return Res.ok();
    }

    /**
     * 更新地址（若为默认地址，需先清楚其他地址默认状态，下同）
     * @param address
     * @return
     */
    @PostMapping("/front/addressUpdate")
    public Res addressUpdate(@RequestBody Address address){
        Long userId = tokenService.getUserId();
        if (address.getIsDefault()==1){
            addressService.updateAddressByUserId(userId);
        }
        addressService.updateAddress(address);
        return Res.ok();
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @GetMapping("/front/addressDelete")
    public Res addressDelete(@RequestParam("id")Long id){

        addressService.deleteAddressById(id);
        return Res.ok();
    }

    /**
     * 地址列表
     * @return
     */
    @GetMapping("/front/addressList")
    public Res addressList(){
        Long userId = tokenService.getUserId();
        Address address = new Address();
        address.setUserId(userId);
        List<Address> list = addressService.selectAddressList(address);

        Long total = new PageInfo(list).getTotal();
        Data data = Data.ok().put("total",total).put("list",list);
        return Res.ok().put("data", data);
    }

    /**
     * 地址详情
     * @param id
     * @return
     */
    @GetMapping("/front/addressInfo")
    public Res addressInfo(@RequestParam("id")Long id){

        Address address = addressService.selectAddressById(id);

        Data data = Data.ok().put("address",address);
        return Res.ok().put("data", data);
    }
}
